<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

// const animationDuration = 6000

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '450px'
    }
  },
  data () {
    return {
      chart: null,
      option: {
        title: {
          text: '实时车位总数统计图',
          subtext: '数据源： 城投集团数据库',
          x: 'center',
          textStyle: {
            color: '#393a3b'
          }
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          },
          formatter: function (params) {
            return params[0].name + '<br/>' +
                   params[0].seriesName + ' : ' + params[0].value + '<br/>' +
                   params[1].seriesName + ' : ' + (params[1].value + params[0].value)
          }
        },
        legend: {
          selectedMode: false,
          data: [
            {
              name: '空余数'
            },
            {
              name: '总数'
            }
          ],
          x: 'right'
        },
        toolbox: {
          show: true,
          feature: {
            mark: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: ['Cosco', 'CMA', 'APL', 'OOCL', 'Wanhai', 'Zim']
          }
        ],
        yAxis: [
          {
            type: 'value',
            boundaryGap: [0, 0.1]
          }
        ],
        series: [
          {
            name: '总数',
            type: 'bar',
            stack: 'sum',
            barCategoryGap: '50%',
            itemStyle: {
              normal: {
                color: 'tomato',
                barBorderColor: 'tomato',
                barBorderWidth: 6,
                barBorderRadius: 0,
                label: {
                  show: true, position: 'insideTop'
                }
              }
            },
            data: [260, 200, 220, 120, 100, 80]
          },
          {
            name: '空余数',
            type: 'bar',
            stack: 'sum',
            itemStyle: {
              normal: {
                color: '#fff',
                barBorderColor: 'tomato',
                barBorderWidth: 6,
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    color: 'tomato'
                  }
                }
              }
            },
            data: [40, 80, 50, 80, 80, 70]
          }
        ]
      }
    }
  },
  mounted () {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption(this.option)
    }
  }
}
</script>
